{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d636f08e",
   "metadata": {},
   "source": [
    "### 第二章4个内置数据结构：列表，字典，元组和集合\n",
    "1. 列表：有序的可变对象集合。\n",
    "- 列表可以动态收缩和扩展到任意大小。\n",
    "- 与数组一样，槽从0开始编号......这些是“索引号”。\n",
    "- 对象存储再列表中的各个槽中。\n",
    "2. 元组：有序的不可变对象集合。\n",
    "3. 无序数据结构：字典：无序的键/值对集合。\n",
    "4. 一种避免重复集合:集合：无序的唯一对象集合。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23aa036e",
   "metadata": {},
   "source": [
    "####  数字：导入random模块，调用random.randint函数来生成1~60的一个随机数，再把这个随机数赋给wait_time变量。\n",
    "#### 字符串：python中的一切都是对象，而且所有的对象都可以赋给变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "baaf6ece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bottles'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word = \"bottles\"\n",
    "word"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca2f32a2",
   "metadata": {},
   "source": [
    "### 列表：用中括号包围，列表中对象用逗号隔开。\n",
    "- 按字面创建列表\n",
    "1. (变量名)prices (赋值操作符)= （字面列表）[]\n",
    "2. 例：温度列表(华氏度)，浮点数列表：temps = [ 32.0, 212.0, 0.0, 81.6, 100.0, 45.3 ]\n",
    "3. 编程中最著名的单词列表：words = [ 'hello', 'word' ]\n",
    "4. 汽车详情信息列表：混合类型数据：car_detailes = [ 'Toyota', 'RAV4', 2.2, 600807]\n",
    "5. everything = [ prices, temps, words, car_details ]\n",
    "6. odds_anh_ends = [ [ 1, 2, 3], ['a', 'b', 'c' ], [ 'one', 'two', 'three'] ]\n",
    "- 案例：测试单词是否包含元音\n",
    "- 一个对象在另一个对象里吗？用“in”来检查。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9e8aaf32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = \"Milliways\"\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1afe220",
   "metadata": {},
   "source": [
    "#### 在运行是“扩展”列表（列表提供了一组内置方法）\n",
    "- \"len\"内置函数会报告一个对象的大小。\n",
    "- “append\"方法增加对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b19333d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = []\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "088df867",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('a')\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5d3e1794",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fcaf03cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('e')\n",
    "found.append('i')\n",
    "found.append('0')\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "94b4d3f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', '0']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2551da6c",
   "metadata": {},
   "source": [
    "#### 用\"in\"检查成员关系：使用In操作符检查一个对象是否包含在一个集合里，还可以使用not in操作符组合来检查一个集合是否不存在某个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bc09fe85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', '0', 'u']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'u' not in found:\n",
    "    found.append('u')\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "405d84e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 原来的“vowels.py”,这个代码会显示“word”中找到的元音\n",
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = \"Milliways\"\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "84b43de2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = \"Milliways\"\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0e2c324",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "45cacf66",
   "metadata": {},
   "source": [
    "### 从列表删除对象\n",
    "- remove:取一个对象值作为唯一参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "942a7ced",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,4]\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "62623ed8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.remove(3)\n",
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dda99487",
   "metadata": {},
   "source": [
    "### 从列表中弹出对象\n",
    "- pop:方法取一个可选的索引值作为参考\n",
    "- pop:无指定对象则弹出列表最后一项，并删除回收此对象,列表收缩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "437e23e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.pop\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "573d0c42",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定一个索引值pop(0),0对应列表第一个对象。\n",
    "nums.pop(0)\n",
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd93cfb9",
   "metadata": {},
   "source": [
    "### 用对象扩展列表\n",
    "- extend方法：取一个对象列表作为唯一参数。合并列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d8fda9c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.extend([3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d07f37b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.extend([])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2b46693",
   "metadata": {},
   "source": [
    "### 在列表中插入一个对象\n",
    "- insert:取一个索引值和一个对象作为参数。.insert(0【插入对象的索引位置】，1【要插入的值】)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b92edc73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 4, 3, 4]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.insert(0,1)\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "172ce8a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 'two-and-a-half', 2, 4, 3, 4]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.insert(2,\"two-and-a-half\")\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6a1c2a84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)# 将phrase字符串转化为一个新列表，这个列表将赋给plist变量。\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):# for循环每次迭代，plist会收缩1个对象，直到最后4个对象都消失。\n",
    "    plist.pop()\n",
    "plist.pop(0)# 调用pop删除列表第一个元素（0索引号）\n",
    "plist.remove(\"'\")#  删除指定元素\n",
    "plist.extend([plist.pop(),plist.pop()])# extend调用将弹出对象添加到plist末尾。\n",
    "plist.insert(2,plist.pop(3))# insert调用把空格字符插入正确的位置\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56b574a0",
   "metadata": {},
   "source": [
    "### 关于列表\n",
    "- 列表很适合储存相关对象的集合。\n",
    "- 列表和其他语言中的数组很类似。但不同的是python列表可以根据需要动态收缩和扩展。\n",
    "- 对象列表用中括号包括，对象用逗号隔开。\n",
    "- 空列表表示为'[]'\n",
    "- 要检查一个对象是否在一个列表里，最快的方法是使用python的in操作符，它会检查成员关系。\n",
    "- append,extend,insert方法可以扩展列表。\n",
    "- remove,pop方法收缩列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5a1e24c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b16b2eef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second = first\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f3f95366",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second.append(6)\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8db79cf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first# bug两个列表同时改变了，共享引用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1caa36f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "third  = second.copy()# copy方法可以完成真正的复制\n",
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "323683cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "third.append(7)# 新对象只增加在此列表\n",
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5bbb8d2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bf2ca89",
   "metadata": {},
   "source": [
    "### 列表扩展中括号记号法\n",
    "- 1.正索引值从左到右数\n",
    "- 2.负索引值从右到左数\n",
    "- 3.第一个值从0开始\n",
    "- 4.start(开始值)：没有指定开始值，则默认为0。\n",
    "- 5.stop(结束值)：没指定结束值，则取列表允许的最大值。\n",
    "- 6.step（步长值）：没指定步长值，则默认为1。\n",
    "- 7.letters[start:stop:step]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8c950c66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "saying = \"Don't panic!\"\n",
    "letters = list(saying)\n",
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "180c36f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cbb54aef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"'\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "03e744ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e9b53b84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bca9bb2",
   "metadata": {},
   "source": [
    "### 切片的使用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8d673ea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "738651ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0:10:3]# 每隔3取1，不包括位置10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c07347e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3:]# 跳过前3个字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4080e16f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[:10]# 直到但不包括索引位置10的所有字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6db8a3c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[::2]# 每2个取1个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dbe08090",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'H',\n",
       " 'i',\n",
       " 't',\n",
       " 'c',\n",
       " 'h',\n",
       " 'h',\n",
       " 'i',\n",
       " 'k',\n",
       " 'e',\n",
       " 'r',\n",
       " \"'\",\n",
       " 's',\n",
       " ' ',\n",
       " 'G',\n",
       " 'u',\n",
       " 'i',\n",
       " 'd',\n",
       " 'e',\n",
       " ' ',\n",
       " 't',\n",
       " 'o',\n",
       " ' ',\n",
       " 't',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'G',\n",
       " 'a',\n",
       " 'l',\n",
       " 'a',\n",
       " 'x',\n",
       " 'y']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book = \"The Hitchhiker's Guide to the Galaxy\"\n",
    "booklist = list(book)\n",
    "booklist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "02a4dbd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[0:3]# 从列表选择前3个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b81cc8eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[0:3])# 将所选范围转换为一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b3bcadb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Galaxy'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[-6:])# 取最后6个对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8003ce08",
   "metadata": {},
   "source": [
    "### 列表中使用步长:切片是列表一个片段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b02e182b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"yxalaG eht ot ediuG s'rekihhctiH ehT\""
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "backwards = booklist[::-1]# 逆序选择\n",
    "''.join(backwards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3430b06a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"TeHthie' ud oteGlx\""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "every_other = booklist[::2]\n",
    "''.join(every_other)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bfa3f488",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hitchhiker'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[4:14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e30def59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rekihhctiH'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[13:3:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27b3d826",
   "metadata": {},
   "source": [
    "### 在列表中使用切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7f22beac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "172e65f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist[1:3])# 首先从“plist”取出单词“on”\n",
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6]])#  挑出其他字母\n",
    "                               #                 t       a         p\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b8ee492",
   "metadata": {},
   "source": [
    "### 列表切片是非破坏性的：原数据仍保持不变。\n",
    "##### ['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
    " #####  0   1   2      3   4   5   6   7   8   9   10     11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "123ab2d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_andorid = \"Marvin\"\n",
    "letters = list(paranoid_andorid)\n",
    "for char in letters:\n",
    "    print('\\t',char)# (\\t)制表符实现列表分行打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "549b7535",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n",
      "\t\t\t  \n"
     ]
    }
   ],
   "source": [
    "paranoid_andorid = \"Marvin，the Paranoid Android\"\n",
    "letters = list(paranoid_andorid)\n",
    "for char in letters[:6]:\n",
    "    print('\\t',char)\n",
    "print()\n",
    "for char in letters[-7:]:\n",
    "    print('\\t'*2,char)\n",
    "print()\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'*3,char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86fe9d9d",
   "metadata": {},
   "source": [
    "### 如果你想储存的数据有一种明确的结构，就要考虑以外的其他数据结构。字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "424478ff",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
